package web1.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.ibatis.session.SqlSession;

import com.alibaba.fastjson.JSON;

import web1.orm.mybatis3.mybatisFactory;

/**
 * Servlet implementation class socreservlet
 */
@WebServlet("/scoreservlet")
public class scoreservlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public scoreservlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		//解决乱码问题
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		//classname要和score.xml中的namespace一致
		String classname = "web1.mybatis3.ScoresMapper.";
		SqlSession sqlSession = mybatisFactory.getSqlSession();
		List<Object> lis = sqlSession.selectList(classname+"selectAllScore");	
		System.out.println(lis);
		System.out.println(lis.get(0));
		String str=JSON.toJSONString(lis);
		response.getWriter().println(str);
        response.getWriter().flush();
        response.getWriter().close();
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
//		doGet(request, response);
		response.setContentType("text/json;charset=utf-8");  
        PrintWriter out = response.getWriter();  
        out.flush();  
        out.close();
	}

}
